package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.domain.SysMarket;
import com.ruoyi.system.domain.SysMarketFile;
import com.ruoyi.system.service.ISysMarketStateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;


@Api(tags = "市场调研审核")
@RestController
@RequestMapping("market/check")
public class SysMarketStateController extends BaseController {

    @Autowired
    private ISysMarketStateService sysMarketStateService;

    @Autowired
    private ServerConfig serverConfig;


    /***
     * 修改市场调研状态:提交文档状态
     * @param
     * @return
     */
    @ApiOperation(value = "修改市场调研状态:提交文档状态")
    @PreAuthorize("@ss.hasPermi('lifecycle:marketResearch:state')")
    @Log(title = "修改市场调研状态:提交文档状态", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/state/upload/{marId}")
    public AjaxResult updateFileState(@PathVariable Long marId) {
        return toAjax(sysMarketStateService.updateFileState(marId));
    }

    /***
     * 修改市场调研项目文件状态:审核通过
     * @param
     * @return
     */
    @ApiOperation(value = "修改项目文档信息状态")
    @PreAuthorize("@ss.hasPermi('lifecycle:marketResearch:update')")
    @Log(title = "修改项目文档信息状态", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/state/yes/{id}")
    public AjaxResult updateState(@PathVariable Long id) {
        return toAjax(sysMarketStateService.updateYesState(id));
    }
    /**
     * 审评
     */
    @ApiOperation(value = "审评")
    @PreAuthorize("@ss.hasPermi('lifecycle:marketResearch:audit')")
    @Log(title = "审评", businessType = BusinessType.UPDATE)
    @PutMapping("/add")
    public AjaxResult edit(@RequestBody SysMarket sysMarket)
    {
        return toAjax(sysMarketStateService.insertSysMarketState(sysMarket));
    }


    /**
     * 上传请求
     */
    @ApiOperation(value = "实现文件上传")
    @Log(title = "文件上传", businessType = BusinessType.IMPORT)
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            String name = file.getOriginalFilename();
            SysMarketFile sysMarket = new SysMarketFile();
            sysMarket.setFileName(name);
            sysMarket.setFileUnique(fileName);
            sysMarket.setFilePath(filePath);
            sysMarket.setUrl(url);
            sysMarket.getSysDeviceId();
            Long fileId = sysMarketStateService.insertPathName(sysMarket);
                ajax.put("fileId", fileId);
                ajax.put("fileName", fileName);
                ajax.put("url", url);
                return ajax;

        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
